package com.andendginegame;

import java.io.IOException;
import java.util.HashMap;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.input.touch.controller.MultiTouch;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchController;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchException;
import org.anddev.andengine.opengl.texture.BuildableTexture;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.builder.BlackPawnTextureBuilder;
import org.anddev.andengine.opengl.texture.builder.ITextureBuilder.TextureSourcePackingException;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import com.andendginegame.HTTPPoster;

import android.widget.TextView;
import android.widget.Toast;

public class AndEngineGame extends BaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	public static final int CAMERA_WIDTH = 720;
	public static final int CAMERA_HEIGHT = 480;
	

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	private BuildableTexture mBloquesTexture;
	static private HashMap<holeType, TextureRegion> mHoleTextures = new HashMap<holeType, TextureRegion>();
	private HashMap<String, Hole> holes = new HashMap<String, Hole>();
	
//	public static final String RED = "RED";
//	public static final String BLUE = "BLUE";
//	public static final String YELLOW = "YELLOW";
//	public static final String GREEN = "GREEN";
//	public static final String BLACK = "BLACK";
//	public static final String BROWN = "BROWN";
//	public static final String ORANGE = "ORANGE";
	private GameManager gameManager = null;
	public static enum holeType {RED,BLUE,YELLOW,GREEN,BLACK,BROWN,ORANGE};
	public boolean multiTouchSupported = false;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

		try {
			if(MultiTouch.isSupported(this)) {
				multiTouchSupported = true;
				engine.setTouchController(new MultiTouchController());
				if(MultiTouch.isSupportedDistinct(this)) {
					Toast.makeText(this, "MultiTouch detected --> Drag multiple Sprites with multiple fingers!", Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(this, "MultiTouch detected --> Drag multiple Sprites with multiple fingers!\n\n(Your device might have problems to distinguish between separate fingers.)", Toast.LENGTH_LONG).show();
				}
			} else {
				Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)", Toast.LENGTH_LONG).show();
			}
		} catch (final MultiTouchException e) {
			Toast.makeText(this, "Sorry your Android Version does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)", Toast.LENGTH_LONG).show();
		}
		
        JSONObject j = new JSONObject();
        try {
        j.put("engineer", "me");
			j.put("date", "today");
        j.put("fuel", "full");
        j.put("car", "mine");
        j.put("distance", "miles");
        
        
        String url = "http://localhost/sesum.php";
        HttpResponse re = HTTPPoster.doPost(url, j);
        String temp = EntityUtils.toString(re.getEntity());
        if (temp.compareTo("SUCCESS")==0)
        {
            Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
        }
        
        
        
        } catch (JSONException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        } catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        
        
        
        TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(R.layout.main);

		return engine;
	}

	public void onLoadResources() {
		
		mBloquesTexture = new BuildableTexture(128, 128,
			      TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		
		TextureRegionFactory.setAssetBasePath("gfx/");
		TextureRegion tr = null;
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "red.png");
		mHoleTextures.put(holeType.RED, tr);

		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "blue.png");
		mHoleTextures.put(holeType.BLUE, tr);
		
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "yellow.png");
		mHoleTextures.put(holeType.YELLOW, tr);
		
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "green.png");
		mHoleTextures.put(holeType.GREEN, tr);
		
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "black.png");
		mHoleTextures.put(holeType.BLACK, tr);
		
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "brown.png");
		mHoleTextures.put(holeType.BROWN, tr);
		
		tr = TextureRegionFactory.createFromAsset(this.mBloquesTexture, this, "orange.png");
		mHoleTextures.put(holeType.ORANGE, tr);
		
		this.mEngine.getTextureManager().loadTexture(this.mBloquesTexture);
		
		try {
			this.mBloquesTexture.build(new BlackPawnTextureBuilder(1));
		} catch (final TextureSourcePackingException e) {
			Debug.e(e);
		}
		
//		this.mEngine.getTextureManager().loadTexture(this.mHoleDeckTexture);
	}

	public Scene onLoadScene() {
		
		final Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(1f, 1f, 1f));
		gameManager = new GameManager(GameManager.GAME_MODE_CLASSIC);
		gameManager.createBoard(scene);
		gameManager.setMultiTouchSupported(this.multiTouchSupported);

//		this.mEngine.registerUpdateHandler(new FPSLogger());

		
		scene.setOnAreaTouchTraversalFrontToBack();



		scene.setTouchAreaBindingEnabled(true);

		return scene;
	}

	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	
//	// ===========================================================
//	// Methods
//	// ===========================================================


	public static HashMap<holeType, TextureRegion> getmHoleTextures() {
		return mHoleTextures;
	}

	public static void setmHoleTextures(HashMap<String, TextureRegion> mHoleTextures) {
		mHoleTextures = mHoleTextures;
	}	
	
}